<template>
  <div style="padding:20px">
    <h1>{{id ? '编辑':'新建'}}召唤师技能</h1>
    <el-form label-width = "120px" :rules = "rule" ref = "FormRef" :model = "model">
      <el-form-item label = "【技能名称】" prop = "name">
        <el-input v-model="model.name" placeholder = "请输入召唤师技能名称" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label = "【技能图标】" prop = "icon">
        <el-upload
          class="avatar-uploader"
          :action="$upload"
          accept = "image/jpg,image/jpeg,image/png"
          :before-upload="$handleBeforeUpload.handleBeforeUpload"
          :show-file-list="false"
          :on-success="afterUpload">
          <img v-if="model.icon" :src="$upload+'/'+model.icon" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label = "【技能展示】" prop = "picture">
        <img v-for = "(item, i) in model.picture" :key = "i" :src="$upload+'/'+item">
        <el-upload
          :action="$upload"
          list-type="picture-card"
          :on-success = "afterUpload1"
          accept = "image/jpg,image/jpeg,image/png"
          :before-upload="$handleBeforeUpload.handleBeforeUpload"
          :on-remove="handleRemove"
          multiple>
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label = "【解锁等级】" prop = "level">
        <el-input v-model="model.level" type = "number" placeholder = "请输入解锁等级" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label = "【技能描述】" prop = "descript">
        <el-input type = "textarea" :rows = "3" v-model="model.descript" placeholder = "请输入召唤师技能描述" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type = 'primary' @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ['id'],
  data () {
    return {
      model: {
        name: '',
        icon: '',
        descript: '',
        level: '',
        picture: []
      },
      rule: {
        name: {
          required: true,
          message: '请填写技能名称',
          trigger: 'blur'
        },
        icon: {
          required: true,
          message: '请上传技能图标',
          trigger: 'blur'
        },
        picture: {
          required: true,
          message: '请上传技能展示',
          trigger: 'blur'
        },
        descript: {
          required: true,
          message: '请填写技能描述',
          trigger: 'blur'
        },
        level: {
          required: true,
          message: '请填写解锁等级',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    async save () {
      this.$refs.FormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.warning('请正确填写所有相关信息')
        }
        const res = this.id ? await this.$http.put('rest/Skill', this.model) : await this.$http.post('rest/Skill', this.model)
        if (res.status !== 200) {
          return this.$message.error(this.id ? '修改召唤师技能失败' : '添加召唤师技能失败')
        }
        this.$store.commit('UpdataSkill', res.data)
        this.$message.success(this.id ? '修改召唤师技能成功' : '添加召唤师技能成功')
        window.sessionStorage.setItem('activePath', '/skill/list')
        this.$router.push('/skill/list')
      })
    },
    afterUpload (res) {
      this.model.icon = res.url
    },
    async fetch () {
      await this.$http.get(`rest/Skill/${this.id}`).then(res => {
        if (res.status !== 200) {
          window.sessionStorage.setItem('activePath', '/skill/list')
          this.$router.push('/skill/list')
          return this.$message.error('获取召唤师技能失败')
        }
        this.model = res.data
      })
    },
    afterUpload1 (res) {
      this.model.picture.push(res.url)
    },
    handleRemove (res) {
      this.model.picture.splice(res.url, 1)
    }
  },
  created () {
    this.id && this.fetch()
  },
  watch: {
    '$route.path': function (newVal) {
      if (newVal === '/skill/create') {
        this.$data.model = { ...this.$options.data().model }
      }
    }
  }
}
</script>
